package com.ruoyi.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.recycle.domain.ExchangeRecord;
import com.ruoyi.recycle.domain.FinanceManagement;
import com.ruoyi.recycle.dto.FinanceManagementDTO;
import com.ruoyi.recycle.mapper.FinanceManagementMapper;
import com.ruoyi.recycle.mapper.IntegralOrderMapper;
import com.ruoyi.recycle.service.ExchangeRecordService;
import com.ruoyi.recycle.service.IFinanceManagementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 财务管理Service业务层处理
 *
 * @author Wubk
 * @date 2022-05-22
 */
@Service
public class FinanceManagementServiceImpl extends ServiceImpl<FinanceManagementMapper, FinanceManagement> implements IFinanceManagementService {
    @Autowired
    private FinanceManagementMapper financeManagementMapper;
    @Autowired
    private IntegralOrderMapper integralOrderMapper;
    @Resource
    private ExchangeRecordService exchangeRecordService;
    @Autowired
    private WechatPaymentService paymentService;


    /**
     * 查询财务管理
     *
     * @param id 财务管理主键
     * @return 财务管理
     */
    @Override
    public FinanceManagement selectFinanceManagementById(Long id) {
        return financeManagementMapper.selectFinanceManagementById(id);
    }

    /**
     * 查询财务管理列表
     *
     * @param financeManagement 财务管理
     * @return 财务管理
     */
    @Override
    public List<FinanceManagement> selectFinanceManagementList(FinanceManagement financeManagement) {
        return financeManagementMapper.selectFinanceManagementList(financeManagement);
    }

    /**
     * 新增财务管理
     *
     * @param financeManagement 财务管理
     * @return 结果
     */
    @Override
    public int insertFinanceManagement(FinanceManagement financeManagement) {
        return financeManagementMapper.insertFinanceManagement(financeManagement);
    }

    /**
     * 修改财务管理
     *
     * @param financeManagement 财务管理
     * @return 结果
     */
    @Override
    public int updateFinanceManagement(FinanceManagement financeManagement) {
        LambdaQueryWrapper<ExchangeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExchangeRecord::getOrderNo,financeManagement.getOrderNumber());
        ExchangeRecord exchangeRecord = exchangeRecordService.getOne(queryWrapper);
        exchangeRecord.setExchangeStatus(financeManagement.getStatus());
        exchangeRecordService.updateById(exchangeRecord);
        return financeManagementMapper.updateFinanceManagement(financeManagement);
    }

    /**
     * 批量删除财务管理
     *
     * @param ids 需要删除的财务管理主键
     * @return 结果
     */
    @Override
    public int deleteFinanceManagementByIds(Long[] ids) {
        return financeManagementMapper.deleteFinanceManagementByIds(ids);
    }

    /**
     * 删除财务管理信息
     *
     * @param id 财务管理主键
     * @return 结果
     */
    @Override
    public int deleteFinanceManagementById(Long id) {
        return financeManagementMapper.deleteFinanceManagementById(id);
    }

    @Override
    public BigDecimal statisticsMoney(Date statTime, Date endTime) {

        return financeManagementMapper.statisticsMoney(statTime, endTime);
    }

    @Override
    public AjaxResult integralWithdrawStatistics(FinanceManagementDTO queryDTO) {

        FinanceManagement financeManagement = new FinanceManagement();
        BeanUtils.copyProperties(queryDTO, financeManagement);
        final LambdaQueryWrapper<FinanceManagement> wrapper = Wrappers.lambdaQuery(financeManagement);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(FinanceManagement::getCreatedTime, queryDTO.getSearchTimeBegin());
        } else if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(FinanceManagement::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        wrapper.like(FinanceManagement::getStatus,"审核通过");
        wrapper.orderByDesc(FinanceManagement::getCreatedTime);
        List<FinanceManagement> integralOrders = financeManagementMapper.selectList(wrapper);
        BigDecimal totalMoney = integralOrders.stream().map(aa -> aa.getWithdrawMoney()).reduce(BigDecimal.ZERO, BigDecimal::add);
        Long totalIntegral = 0l;
        for (FinanceManagement integralOrder : integralOrders) {
            totalIntegral+=integralOrder.getWithdrawIntegral();
        }

        Page<FinanceManagement> financeManagementPage = this.page(new Page<FinanceManagement>().setCurrent(queryDTO.getCurrentPage()).setSize(queryDTO.getPageSize()), wrapper);


        return AjaxResult.success().put("integralOrders", financeManagementPage).put("wxMoney", totalMoney).put("totalIntegral",totalIntegral);
    }
}
